# 享元模式
# => ⭐运用共享技术有效地支持大量细粒度的对象。

# 享元模式结构中包含如下角色：
# 1. FlyWeight（享元）：一个享元就是一个可共享的对象，它可以被多个客户端共享。
# 2. FlyWeightFactory（享元工厂）：负责创建和管理享元。
# 3. Client（客户端）：使用享元工厂来获取享元对象，并通过享元对象来完成操作。
class FlyWeight:
    def __init__(self, intrinsic_state):
        self.intrinsic_state = intrinsic_state

    def operation(self, extrinsic_state):
        print(self.intrinsic_state, extrinsic_state)

class FlyWeightFactory:
    def __init__(self):
        self.flyweights = {}
    def get_flyweight(self, key):
        if key not in self.flyweights:
            self.flyweights[key] = FlyWeight(key)
        return self.flyweights[key]
if __name__ == "__main__":
    factory = FlyWeightFactory()
    fw1 = factory.get_flyweight("state1")
    fw3 = factory.get_flyweight("state1")
    fw2 = factory.get_flyweight("state2")
    fw1.operation("extrinsic_state1")
    fw2.operation("extrinsic_state2")
    fw3.operation("extrinsic_state3")
    # Output: state1 extrinsic_state1 state2 extrinsic_state2